package lishe.myrefreshview.MyResyclerView;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by shino on 2017/9/22.
 */

public abstract class RecycleBaseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private View headerView;
    private View footView;
    private View refreshHeaderView;
    private View refreshFooterView;

    private static final int TYPE_HEADER = -10000;
    private static final int TYPE_FOOTER = 10000;
    private static final int TYPE_REFRESH_HEADER = -10001;
    private static final int TYPE_REFRESH_FOOTER = 10001;
//------------------------------------头部底部view--------------------------------

    public void addHeaderView(View headerView) {
        this.headerView = headerView;
    }

    public View getHeaderView() {
        return headerView;
    }

    public void addRefreshHeaderView(View refreshHeaderView) {
        this.refreshHeaderView = refreshHeaderView;
    }

    public View getRefreshHeaderView() {
        return refreshHeaderView;
    }


    public void addFootView(View footView) {
        this.footView = footView;
    }

    public View getFootView() {
        return footView;
    }

    public void addRefreshFootView(View refreshFootView) {
        this.refreshFooterView = refreshFootView;
    }

    public View getRefreshFootView() {
        return refreshFooterView;
    }

    public final void removeHeaderRefreshView() {
        refreshHeaderView = null;
    }

    public final void removeFooterRefreshView() {
        refreshFooterView = null;
    }

    //--------------------------------recycleview 的原有函数-使用final修饰-----------------------------------------
    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            if (refreshHeaderView != null) {
                return TYPE_REFRESH_HEADER;
            } else if (headerView != null) {
                return TYPE_HEADER;
            }
        }
        if (position == 1 && refreshHeaderView != null && headerView != null) {
            return TYPE_HEADER;
        }
        if (position == getItemCount() - 1) {
            if (refreshFooterView != null) {
                return TYPE_REFRESH_FOOTER;
            } else if (footView != null) {
                return TYPE_FOOTER;
            }
        }
        if (refreshFooterView != null && footView != null && position == getItemCount() - 2) {
            return TYPE_FOOTER;
        }
        return super.getItemViewType(position);
    }

    @Override
    public final RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            return new ViewHolder(headerView);
        } else if (viewType == TYPE_FOOTER) {
            return new ViewHolder(footView);
        } else if (viewType == TYPE_REFRESH_HEADER) {
            return new ViewHolder(refreshHeaderView);
        } else if (viewType == TYPE_REFRESH_FOOTER) {
            return new ViewHolder(refreshFooterView);
        }
        return onCreateViewHolderMy(parent, viewType);
    }

    @Override
    public final void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int headerCount = 0;
        if (headerView != null) {
            headerCount = headerCount + 1;
        }
        if (refreshHeaderView != null) {
            headerCount = headerCount + 1;
        }
        //type等于头部或底部的type册不调用onBindViewHolderMy
        int type = getItemViewType(position);
        if (type == TYPE_HEADER || type == TYPE_FOOTER || type == TYPE_REFRESH_HEADER || type == TYPE_REFRESH_FOOTER) {
            return;
        }
        setOnItemListener(holder, position - headerCount);
        onBindViewHolderMy(holder, position - headerCount);
    }

    @Override
    public final int getItemCount() {
        int itemCount = 0;
        if (headerView != null) {
            itemCount = itemCount + 1;
        }
        if (footView != null) {
            itemCount = itemCount + 1;
        }
        if (refreshHeaderView != null) {
            itemCount = itemCount + 1;
        }
        if (refreshFooterView != null) {
            itemCount = itemCount + 1;
        }
        return getItemCountMy() + itemCount;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int type = getItemViewType(position);
                    if (type == TYPE_HEADER || type == TYPE_FOOTER || type == TYPE_REFRESH_FOOTER || type == TYPE_REFRESH_HEADER) {
                        return gridManager.getSpanCount();
                    }
                    return 1;
                }
            });
        }
    }

    //------------------------对应recycleview必须实现的三个抽象方法-----final修饰了原有的方法，防止误用----------------------
    public abstract RecyclerView.ViewHolder onCreateViewHolderMy(ViewGroup parent, int viewType);

    public abstract void onBindViewHolderMy(RecyclerView.ViewHolder holder, int position);

    public abstract int getItemCountMy();


    //实现点击事件
    private void setOnItemListener(final RecyclerView.ViewHolder holder, final int position) {
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (itemLongClickListener != null) {
                    return itemLongClickListener.OnLongClickListener(RecycleBaseAdapter.this, holder.itemView, position);
                }
                return false;
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemClicListener != null) {
                    itemClicListener.OnClickListener(RecycleBaseAdapter.this, holder.itemView, position);
                }
            }
        });
    }

    /**
     * 头部viwe 的holder
     */
    class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
            //----------------避免存在parent，导致recycleview，addView报错------------------------
            ViewGroup view = (ViewGroup) itemView.getParent();
            if (view != null) {
                view.removeAllViews();
            }
        }
    }

    //---------------------------------------------点击监听事件---------------------------------
    ItemListener.ItemClickListener itemClicListener;
    ItemListener.ItemLongClickListener itemLongClickListener;

    interface ItemListener {
        interface ItemClickListener {
            void OnClickListener(RecyclerView.Adapter adapter, View view, int position);
        }

        interface ItemLongClickListener {
            boolean OnLongClickListener(RecyclerView.Adapter adapter, View view, int position);
        }
    }

    public void setOnItemClickListener(ItemListener.ItemClickListener itemClicListener) {
        this.itemClicListener = itemClicListener;
    }

    public void setOnItemLongClickListener(ItemListener.ItemLongClickListener itemLongClickListener) {
        this.itemLongClickListener = itemLongClickListener;
    }
}
